package org.tarena.dang.action.main;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.tarena.dang.action.BaseAction;
import org.tarena.dang.entity.Categrory;
import org.tarena.dang.service.CategoryService;
import org.tarena.dang.service.CategoryServiceImp;

public class CategoryAction extends BaseAction {
	//inpu无
	//output
	
	private List<Categrory> cats;
		
	public String execute(){
		CategoryService categoryService = new CategoryServiceImp();
		try {
			List<Categrory> all = categoryService.findAll();
			//将小说....计算机给cats;
			cats = findByParent(all,1);
			System.out.println("图书共分为----"+cats.size()+"----大类");
			for(Categrory c:cats){
				List<Categrory> subCats = findByParent(all, c.getId());
				c.setSubCats(subCats);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	
	private List<Categrory> findByParent(List<Categrory> all, int i) {
		List<Categrory> list = new ArrayList<Categrory>();
		for(Categrory c:all){
			if(c.getParentId()==i){
				list.add(c);
			}
		}
		
		return list;
	}


	public List<Categrory> getCats() {
		
		return cats;
	}

	public void setCats(List<Categrory> cats) {
		this.cats = cats;
	}
		
}
